﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using ESF.General.MVP;
using System.Windows.Threading;
using ESF.General;
using System.Windows.Controls;
using ESF.General.GUI;
using ESF.WinForms.Dialogs;
using System.Windows.Controls.Primitives;

#endregion


namespace ESF.WinForms.Elements
{
	public class BaseWPFWindow
		: Window, IBaseViewer
	{
		#region Members

		protected IProgressDialog<Window> ProgressDialog;

		#endregion


		#region IBaseViewer Fields

		public string frmUserHostAddress { get { return Log.UserHostAddress; } }


		public string frmErrorMsg
		{
			set
			{
				Dispatcher.Invoke (
					(Action)(() => 
					{
						System.Windows.MessageBox.Show (this, value, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
					}));
			}
		}

		public string frmWarnMsg
		{
			set
			{
				Dispatcher.Invoke(
					(Action)(() =>
					{
						System.Windows.MessageBox.Show(this, value, "Tenga en cuenta", MessageBoxButton.OK, MessageBoxImage.Warning);
					}));
			}
		}


		public string frmInfoMsg
		{
			set
			{
				Dispatcher.Invoke(
					(Action)(() =>
					{
						System.Windows.MessageBox.Show(this, value, "Información", MessageBoxButton.OK, MessageBoxImage.Information);
					}));
			}
		}

		public bool frmIsAuthenticated
		{
			get { return false; }
		}

		public string frmUserAlias
		{
			get { return string.Empty; }
		}


		public int CurrentAdvance100 { set { ProgressDialog.PercentageAdvance100 = value; } }

		public string CurrentStepName { set { ProgressDialog.CurrentStepName = value; } }

		#endregion


		#region Constructor

		public BaseWPFWindow () : base ()
		{
			ProgressDialog = new ProgressDialogWPF ();
			ProgressDialog.OnCancel += CancelCurrentOperation;
		}

		#endregion
		

		#region Methods
		
		public void IncludeVersionInTitle ()
		{
			if (Title.EndsWith (ReadConfig.ExecutingVersion))
			{
				return;
			}

			if (string.IsNullOrWhiteSpace(Title))
			{
				Title = ReadConfig.ApplicationName;
			}

			Title += " - versión: " + ReadConfig.ExecutingVersion;
		}


		protected virtual void CancelCurrentOperation () {}


		protected virtual void Clear () {}


		public void ShowClearProgressDialog ()
		{
			Dispatcher.Invoke ((Action) delegate 
			{  
				ProgressDialog.Clear ();

				if (this.IsVisible)
				{
					ProgressDialog.Show (this);		
				}
			});
		}
		
		
		public MessageBoxResult ShowConfirm (string pMessage)
		{
			MessageBoxResult result = MessageBoxResult.Cancel;


			Dispatcher.Invoke (
				(Action)(() => 
				{
					result = System.Windows.MessageBox.Show(this, pMessage, "Confirmación", MessageBoxButton.YesNo, MessageBoxImage.Question);
				}));

			return result;
		}


		public void CloseApplication()
		{
			Dispatcher.Invoke (
				(Action)(() => 
				{
					System.Windows.Application.Current.Shutdown ();
				}));
		}


		public string GetText(Label pLabel)
		{
			return WPFHelper.GetText (pLabel);
		}


		public string GetText (TextBox pText) 
		{
			return WPFHelper.GetText (pText);
		}


		public string GetText(TextBlock pText)
		{
			return WPFHelper.GetText(pText);
		}


		public string GetPassword(PasswordBox pPasBox)
		{
			return WPFHelper.GetPassword (pPasBox);
		}


		public void SetText(Label pLabel, string pText)
		{
			WPFHelper.SetText (pLabel, pText);
		}


		public void SetText (TextBox pTextBox, string pText)
		{
			WPFHelper.SetText (pTextBox, pText);
		}


		public void ClearPassword(PasswordBox pPasBox)
		{
			WPFHelper.ClearPassword (pPasBox);
		}


		public void SetValue (ProgressBar pBar, int pValue100)
		{
			WPFHelper.SetValue (pBar, pValue100);
		}


		public void Focus (Control pControl)
		{
			WPFHelper.Focus (pControl);
		}


		public void LogError (Exception pEx)
		{
			Dispatcher.Invoke ((Action) delegate () { ProgressDialog.Hide (); });

			Log.Error (pEx);
			
			frmErrorMsg = pEx.Message;
		}


		public void SetAdvance(string pMessage = "", int pTotalWorkCount = 0, int pReadyWorkCount = 0, int pTotalErrors = 0, long pItemElapsedMs = 0)
		{
			double advance100;


			advance100 = ProcessHelper.GetAdvancePercentage100(pTotalWorkCount, pReadyWorkCount + pTotalErrors);

			CurrentStepName =
				(string.IsNullOrEmpty(pMessage) ? string.Empty : pMessage + "... ") +
				string.Format("Procesados {0} de {1}{3}: {2:0.##}%", pReadyWorkCount, pTotalWorkCount, advance100,
				(pTotalErrors > 0 ? string.Format(" con {0} errores", pTotalErrors) : string.Empty));

			CurrentAdvance100 = (int)advance100;
		}


		public void SetAdvanceCompleted()
		{
			CurrentAdvance100 = 100;
		}

		#endregion
	}
}
